﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace CH_k
{
    [Serializable]
    public class SOURCE : Element
    {
        public bool state;

        public SOURCE()
        {
            state = true;
            size = new Point(20, 20);
            ElType = "SOURCE";
        }
        public void ON()
        {
            state = true;
        }
        public void OFF()
        {
            state = false;
        }
        public void change()
        {
            state = !state;
        }
        public override bool Run()
        {
            return state;
        }
        public override void Delete()
        {
            for (int i = 0; i < outElList.Count; ++i)
            {
                if (outElList[i] != null)
                {
                    Element tRef = outElList[i];
                    outElList[i].Delete();
                    seList.Remove(tRef);
                    --i;
                }
            }
        }
        public override bool ClickLeft(Point MouseCord)
        {
            if (MouseCord.X > dec.X && MouseCord.X < dec.X + size.X)
            {
                if (MouseCord.Y > dec.Y && MouseCord.Y < dec.Y + size.Y)
                {
                    change();
                    return true;
                }
            }
            return false;
        }
        public override void ELineUpdate()
        {
            foreach (Element el in outElList)
            {
                el.input_p = new Point(dec.X + 30, dec.Y + 10);
                el.pCalc();
            }
        }
        public override bool hover(Point MouseCord)
        {
            return ((MouseCord.X > dec.X && MouseCord.X < dec.X + 10 + size.X) && (MouseCord.Y > dec.Y && MouseCord.Y < dec.Y + size.Y));
        }
        public override void TryClickOutput(Point TCord)
        {
            if ((TCord.X == dec.X + 30) && (TCord.Y == dec.Y + 10))
            {
                if (Element.newElement.ElType == "ELine")
                {
                    Element.newElement.TrySetInput(TCord, this);
                }
            }
        }
        public override void Draw(Graphics _g)
        {
            if (cPen == null) cPen = new Pen(Color.Black);
            _g.FillEllipse(Brushes.LightGray, dec.X, dec.Y, 20, 20);
            _g.DrawEllipse(cPen, dec.X, dec.Y, 20, 20);
            _g.DrawString(state ? "1" : "0", new Font("Arial", 12), Brushes.Black, dec.X + 3, dec.Y + 1);
            _g.DrawLine(cPen, dec.X + 20, dec.Y + 10, dec.X + 30, dec.Y + 10);
        }
    }
}
